package su.security.module;

/**
 * 权限用户信息管理
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.kuakao.core.dto.PagerDTO;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import su.security.entity.SecurityModule;
import su.security.entity.SecurityUser;
import su.security.service.SecurityModuleService;
import su.security.service.SecurityUserService;
import su.tool.SuIntUtils;
@Controller
@RequestMapping("/admin/securityUser")
public class SecurityUserServlet {

	@Autowired
	private SecurityUserService securityUserService;

	@Autowired
	private SecurityModuleService securityModuleService;

	private final Log log = LogFactory.getLog(getClass());
	/**
	 * 首页
	 * @param request
	 * @param model
	 * @param dto
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("index")
	public String index(HttpServletRequest request, Model model,PagerDTO dto) {
		List<SecurityUser> users = securityUserService.queryByPage(dto);
		model.addAttribute("users", users);
		model.addAttribute("_page", dto);
		return "/maintain/security/user/user_index";
	}

	/**
	 * 到添加页面
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("to_add")
	public String toAdd(HttpServletRequest request, Model model) {
		List<SecurityModule> allModules = securityModuleService.getAllModuleList();
		List<Map> cbs = null;//checkbox参数
		if(allModules!=null && !allModules.isEmpty()){
			cbs = new ArrayList<Map>();
			for(SecurityModule m : allModules){
				Map map = new HashMap();
				map.put("cbValue", m.getId());
				map.put("cbShow", m.getModulename());
				map.put("checked", "false");
				cbs.add(map);
			}
		}
		
		model.addAttribute("cbs", cbs);
		return "/maintain/security/user/user_edit";
	}
	
	/**
	 * 保存
	 * @param request
	 * @param model
	 * @param user
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("do_save")
	public String doSave(HttpServletRequest request, Model model,SecurityUser user){
		String res = "";
		try {
			securityUserService.save(user);
		} catch (Exception e) {
			res = e.getMessage();
		}
		if(StringUtils.isNotBlank(res)){
			request.setAttribute("errorCode", res);
			return "su_error";
		}
		return "redirect:/admin/securityUser/index.action";
	}
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @param model
	 * @throws Exception
	 */
	@RequestMapping("do_remove")
	public void doRemove(HttpServletRequest request,HttpServletResponse response, Model model) {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
        PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e1) {
			log.error(e1.getMessage());
		}
		Map<String,String> msgs = new HashMap<String,String>();
		
        int id = SuIntUtils.getInt(request.getParameter("id"));
        if(id==0){
			msgs.put("no id", "请选择要删除的数据");
		}else{
			String msg = "";
			try {
				securityUserService.remove(id);
			} catch (Exception e) {
				msg = e.getMessage();
			}
			if(StringUtils.isNotBlank(msg)){
				msgs.put("service error", msg);
			}else{
				msgs.put("success", "success");
			}
		}
		
		writer.print(JSONObject.fromObject(msgs));
		writer.flush();
		writer.close();
    }
	/**
	 * 到修改页面
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("to_edit")
	public String toEdit(HttpServletRequest request, Model model){
        int id = SuIntUtils.getInt(request.getParameter("id"));
        SecurityUser baseinfo = securityUserService.getBaseinfo(id);
        model.addAttribute("baseinfo", baseinfo);
        //checkbox参数
        String moduleids = baseinfo.getModuleids();
        Map<String,String> moduleidMap = new HashMap<String, String>();
        if(StringUtils.isNotBlank(moduleids)){
        	for(String us : moduleids.split(",")){
        		moduleidMap.put(us, us);
        	}
        }
        List<SecurityModule> allModules = securityModuleService.getAllModuleList();
		List<Map> cbs = null;
		if(allModules!=null && !allModules.isEmpty()){
			cbs = new ArrayList<Map>();
			for(SecurityModule m : allModules){
				Map map = new HashMap();
				map.put("cbValue", m.getId());
				map.put("cbShow", m.getModulename());
				if(moduleidMap.containsKey(String.valueOf(m.getId()))){
					map.put("checked", "true");
				}else{
					map.put("checked", "false");
				}
				
				cbs.add(map);
			}
		}
		model.addAttribute("cbs", cbs);
		return "/maintain/security/user/user_edit";
	}

}
